package com.components.base.titlebar

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import com.components.base.R
import com.components.base.titlebar.style.LightBarStyle
import com.components.base.titlebar.style.NightBarStyle
import com.components.base.titlebar.style.RippleBarStyle
import com.components.base.titlebar.style.TransparentBarStyle
import kotlin.math.max

class TitleBar : FrameLayout, View.OnClickListener, View.OnLayoutChangeListener {

    companion object {
        /**默认初始化器*/
        private var sGlobalStyle: ITitleBarStyle = LightBarStyle()
    }

    /**当前初始化器*/
    private var mCurrentStyle: ITitleBarStyle

    /**监听器对象*/
    private var mListener: OnTitleBarListener? = null

    /**标题栏子控件*/
    private var mLeftView: TextView
    private var mTitleView: TextView
    private var mRightView: TextView
    private var mLineView: View

    /**控件内间距*/
    private var mHorizontalPadding: Int = 0
    private var mVerticalPadding: Int = 0

    /**图标显示大小*/
    private var mLeftIconWidth: Int = 0
    private var mLeftIconHeight: Int = 0
    private var mTitleIconWidth: Int = 0
    private var mTitleIconHeight: Int = 0
    private var mRightIconWidth: Int = 0
    private var mRightIconHeight: Int = 0

    /**图标显示重心*/
    private var mLeftIconGravity: Int = 0
    private var mTitleIconGravity = 0
    private var mRightIconGravity = 0

    /**图标着色器*/
    private var mLeftIconTint = 0
    private var mTitleIconTint = 0
    private var mRightIconTint = TitleBarSupport.NO_COLOR

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    @SuppressLint("CustomViewStyleable")
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        val array = context.obtainStyledAttributes(attrs, R.styleable.titleBar, 0, R.style.titleBarStyle)
        mCurrentStyle = when (array.getInt(R.styleable.titleBar_barStyle, 0)) {
            0x10 -> LightBarStyle()
            0x20 -> NightBarStyle()
            0x30 -> TransparentBarStyle()
            0x40 -> RippleBarStyle()
            else -> sGlobalStyle
        }

        mTitleView = mCurrentStyle.createTitleView(context)
        mLeftView = mCurrentStyle.createLeftView(context)
        mRightView = mCurrentStyle.createRightView(context)
        mLineView = mCurrentStyle.createLineView(context)

        mTitleView.layoutParams = LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.MATCH_PARENT,
            Gravity.CENTER_HORIZONTAL
        )
        mLeftView.layoutParams = LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.MATCH_PARENT,
            Gravity.START
        )
        mRightView.layoutParams = LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.MATCH_PARENT,
            Gravity.END
        )

        mLineView.layoutParams = LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            mCurrentStyle.getLineSize(context),
            Gravity.BOTTOM
        )
        /**设置图标显示的重心*/
        setTitleIconGravity(
            array.getInt(
                R.styleable.titleBar_titleIconGravity,
                mCurrentStyle.getTitleIconGravity(context)
            )
        )
        setLeftIconGravity(
            array.getInt(
                R.styleable.titleBar_leftIconGravity,
                mCurrentStyle.getLeftIconGravity(context)
            )
        )
        setRightIconGravity(
            array.getInt(
                R.styleable.titleBar_rightIconGravity,
                mCurrentStyle.getRightIconGravity(context)
            )
        )

        /**设置图标显示的大小*/
        setTitleIconSize(
            array.getDimensionPixelSize(R.styleable.titleBar_titleIconWidth, mCurrentStyle.getTitleIconWidth(context)),
            array.getDimensionPixelSize(R.styleable.titleBar_titleIconHeight, mCurrentStyle.getTitleIconHeight(context))
        )
        setLeftIconSize(
            array.getDimensionPixelSize(R.styleable.titleBar_leftIconWidth, mCurrentStyle.getLeftIconWidth(context)),
            array.getDimensionPixelSize(R.styleable.titleBar_leftIconHeight, mCurrentStyle.getLeftIconHeight(context))
        )
        setRightIconSize(
            array.getDimensionPixelSize(R.styleable.titleBar_rightIconWidth, mCurrentStyle.getRightIconWidth(context)),
            array.getDimensionPixelSize(R.styleable.titleBar_rightIconHeight, mCurrentStyle.getRightIconHeight(context))
        )

        /**设置文字和图标之间的间距*/
        setTitleIconPadding(array.getDimensionPixelSize(R.styleable.titleBar_titleIconPadding, mCurrentStyle.getTitleIconPadding(context)))
        setLeftIconPadding(array.getDimensionPixelSize(R.styleable.titleBar_leftIconPadding, mCurrentStyle.getLeftIconPadding(context)))
        setRightIconPadding(array.getDimensionPixelSize(R.styleable.titleBar_rightIconPadding, mCurrentStyle.getRightIconPadding(context)))

        /**标题设置*/
        if (array.hasValue(R.styleable.titleBar_title)) {
            (if (array.getResourceId(
                    R.styleable.titleBar_title,
                    0
                ) != R.string.bar_string_placeholder
            ) array.getString(R.styleable.titleBar_title) else mCurrentStyle.getTitle(context))?.let {
                setTitle(
                    it
                )
            }
        }

        if (array.hasValue(R.styleable.titleBar_leftTitle)) {
            (if (array.getResourceId(
                    R.styleable.titleBar_leftTitle,
                    0
                ) != R.string.bar_string_placeholder
            ) array.getString(R.styleable.titleBar_leftTitle) else mCurrentStyle.getLeftTitle(
                context
            ))?.let {
                setLeftTitle(
                    it
                )
            }
        }

        if (array.hasValue(R.styleable.titleBar_rightTitle)) {
            (if (array.getResourceId(
                    R.styleable.titleBar_rightTitle,
                    0
                ) != R.string.bar_string_placeholder
            ) array.getString(R.styleable.titleBar_rightTitle) else mCurrentStyle.getRightTitle(
                context
            ))?.let {
                setRightTitle(
                    it
                )
            }
        }

        /**图标着色设置*/
        if (array.hasValue(R.styleable.titleBar_titleIconTint)) {
            setTitleIconTint(array.getColor(R.styleable.titleBar_titleIconTint, 0))
        }

        if (array.hasValue(R.styleable.titleBar_leftIconTint)) {
            setLeftIconTint(array.getColor(R.styleable.titleBar_leftIconTint, 0))
        }

        if (array.hasValue(R.styleable.titleBar_rightIconTint)) {
            setRightIconTint(array.getColor(R.styleable.titleBar_rightIconTint, 0))
        }

        /**图标设置*/
        if (array.hasValue(R.styleable.titleBar_titleIcon)) {
            setTitleIcon(
                TitleBarSupport.getDrawable(
                    context,
                    array.getResourceId(R.styleable.titleBar_titleIcon, 0)
                )
            )
        }

        if (array.hasValue(R.styleable.titleBar_leftIcon)) {
            if (array.getResourceId(
                    R.styleable.titleBar_leftIcon,
                    0
                ) != R.drawable.bar_drawable_placeholder
            ) {
                val drawable = TitleBarSupport.getDrawable(
                    context,
                    array.getResourceId(R.styleable.titleBar_leftIcon, 0)
                )
                setLeftIcon(drawable)
            } else {
                val backButtonDrawable = mCurrentStyle.getBackButtonDrawable(context)
                setLeftIcon(backButtonDrawable)
            }
        }

        if (array.hasValue(R.styleable.titleBar_rightIcon)) {
            setRightIcon(
                TitleBarSupport.getDrawable(
                    context,
                    array.getResourceId(R.styleable.titleBar_rightIcon, 0)
                )
            )
        }

        /**文字颜色设置*/
        setTitleColor(
            (if (array.hasValue(R.styleable.titleBar_titleColor)) array.getColorStateList(
                R.styleable.titleBar_titleColor
            ) else mCurrentStyle.getTitleColor(context))!!
        )
        setLeftTitleColor(
            (if (array.hasValue(R.styleable.titleBar_leftTitleColor)) array.getColorStateList(
                R.styleable.titleBar_leftTitleColor
            ) else mCurrentStyle.getLeftTitleColor(context))!!
        )
        setRightTitleColor(
            (if (array.hasValue(R.styleable.titleBar_rightTitleColor)) array.getColorStateList(
                R.styleable.titleBar_rightTitleColor
            ) else mCurrentStyle.getRightTitleColor(context))!!
        )

        /**文字大小设置*/
        setTitleSize(
            TypedValue.COMPLEX_UNIT_PX,
            if (array.hasValue(R.styleable.titleBar_titleSize)) array.getDimensionPixelSize(
                R.styleable.titleBar_titleSize,
                0
            ) else mCurrentStyle.getTitleSize(context)
        )
        setLeftTitleSize(
            TypedValue.COMPLEX_UNIT_PX,
            if (array.hasValue(R.styleable.titleBar_leftTitleSize)) array.getDimensionPixelSize(
                R.styleable.titleBar_leftTitleSize,
                0
            ) else mCurrentStyle.getLeftTitleSize(context)
        )
        setRightTitleSize(
            TypedValue.COMPLEX_UNIT_PX,
            if (array.hasValue(R.styleable.titleBar_rightTitleSize)) array.getDimensionPixelSize(
                R.styleable.titleBar_rightTitleSize,
                0
            ) else mCurrentStyle.getRightTitleSize(context)
        )

        /**文字样式设置*/
        val titleStyle = if (array.hasValue(R.styleable.titleBar_titleStyle)) array.getInt(
            R.styleable.titleBar_titleStyle,
            Typeface.NORMAL
        ) else mCurrentStyle.getTitleStyle(context)
        setTitleStyle(mCurrentStyle.getTitleTypeface(context, titleStyle), titleStyle)

        val leftTitleStyle = if (array.hasValue(R.styleable.titleBar_leftTitleStyle)) array.getInt(
            R.styleable.titleBar_leftTitleStyle,
            Typeface.NORMAL
        ) else mCurrentStyle.getLeftTitleStyle(context)
        setLeftTitleStyle(
            mCurrentStyle.getLeftTitleTypeface(context, leftTitleStyle),
            leftTitleStyle
        )

        val rightTitleStyle =
            if (array.hasValue(R.styleable.titleBar_rightTitleStyle)) array.getInt(
                R.styleable.titleBar_rightTitleStyle,
                Typeface.NORMAL
            ) else mCurrentStyle.getRightTitleStyle(context)
        setRightTitleStyle(
            mCurrentStyle.getRightTitleTypeface(context, rightTitleStyle),
            rightTitleStyle
        )

        /**标题重心设置*/
        if (array.hasValue(R.styleable.titleBar_titleGravity)) {
            setTitleGravity(array.getInt(R.styleable.titleBar_titleGravity, Gravity.NO_GRAVITY))
        }

        /**背景设置*/
        if (array.hasValue(R.styleable.titleBar_android_background)) {
            if (array.getResourceId(
                    R.styleable.titleBar_android_background,
                    0
                ) == R.drawable.bar_drawable_placeholder
            ) {
                TitleBarSupport.setBackground(
                    this,
                    mCurrentStyle.getTitleBarBackground(context)
                )
            }
        }
        if (array.hasValue(R.styleable.titleBar_leftBackground)) {
            setLeftBackground(
                (if (array.getResourceId(
                        R.styleable.titleBar_leftBackground,
                        0
                    ) != R.drawable.bar_drawable_placeholder
                ) array.getDrawable(R.styleable.titleBar_leftBackground) else mCurrentStyle.getLeftTitleBackground(
                    context
                ))!!
            )
        }

        if (array.hasValue(R.styleable.titleBar_rightBackground)) {
            setRightBackground(
                (if (array.getResourceId(
                        R.styleable.titleBar_rightBackground,
                        0
                    ) != R.drawable.bar_drawable_placeholder
                ) array.getDrawable(R.styleable.titleBar_rightBackground) else mCurrentStyle.getRightTitleBackground(
                    context
                ))!!
            )
        }

        /**分割线设置*/
        setLineVisible(
            array.getBoolean(
                R.styleable.titleBar_lineVisible,
                mCurrentStyle.isLineVisible(context)
            )
        )

        if (array.hasValue(R.styleable.titleBar_lineDrawable)) {
            setLineDrawable(
                (if (array.getResourceId(
                        R.styleable.titleBar_lineDrawable,
                        0
                    ) != R.drawable.bar_drawable_placeholder
                ) array.getDrawable(R.styleable.titleBar_lineDrawable) else mCurrentStyle.getLineDrawable(
                    context
                ))!!
            )
        }

        if (array.hasValue(R.styleable.titleBar_lineSize)) {
            setLineSize(array.getDimensionPixelSize(R.styleable.titleBar_lineSize, 0))
        }

        /**设置子控件的内间距*/
        mHorizontalPadding = array.getDimensionPixelSize(
            R.styleable.titleBar_childPaddingHorizontal,
            mCurrentStyle.getChildHorizontalPadding(context)
        )
        mVerticalPadding = array.getDimensionPixelSize(
            R.styleable.titleBar_childPaddingVertical,
            mCurrentStyle.getChildVerticalPadding(context)
        )
        setChildPadding(mHorizontalPadding, mVerticalPadding)

        /**回收 TypedArray 对象*/
        array.recycle()

        addView(mTitleView, 0)
        addView(mLeftView, 1)
        addView(mRightView, 2)
        addView(mLineView, 3)

        addOnLayoutChangeListener(this)

        /**如果当前是布局预览模式*/
        if (isInEditMode) {
            measure(0, 0)
            mTitleView.measure(0, 0)
            mLeftView.measure(0, 0)
            mRightView.measure(0, 0)
            val horizontalMargin =
                max(mLeftView.measuredWidth, mRightView.measuredWidth) + mHorizontalPadding
            val layoutParams = mTitleView.layoutParams as MarginLayoutParams
            layoutParams.setMargins(horizontalMargin, 0, horizontalMargin, 0)
        }
    }

    override fun onClick(view: View?) {
        if (mListener == null) {
            return
        }
        if (view === mLeftView) {
            mListener!!.onLeftClick(this)
        } else if (view === mRightView) {
            mListener!!.onRightClick(this)
        } else if (view === mTitleView) {
            mListener!!.onTitleClick(this)
        }
    }

    override fun onLayoutChange(
        v: View?,
        left: Int,
        top: Int,
        right: Int,
        bottom: Int,
        oldLeft: Int,
        oldTop: Int,
        oldRight: Int,
        oldBottom: Int
    ) {
        /**先移除当前的监听，因为 TextView.setMaxWidth 方法会重新触发监听*/
        removeOnLayoutChangeListener(this)

        if (mLeftView.maxWidth != Int.MAX_VALUE && mTitleView.maxWidth != Int.MAX_VALUE && mRightView.maxWidth != Int.MAX_VALUE) {
            /**不限制子 View 的宽度*/
            mLeftView.maxWidth = Int.MAX_VALUE
            mTitleView.maxWidth = Int.MAX_VALUE
            mRightView.maxWidth = Int.MAX_VALUE
            /**对子 View 重新进行测量*/
            mLeftView.measure(
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
            )
            mTitleView.measure(
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
            )
            mRightView.measure(
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
            )
        }
        /**标题栏子 View 最大宽度限制算法*/
        val barWidth = right - left
        val sideWidth = max(mLeftView.measuredWidth, mRightView.measuredWidth)
        val maxWidth = sideWidth * 2 + mTitleView.measuredWidth
        /**算出来子 View 的宽大于标题栏的宽度*/
        if (maxWidth >= barWidth) {
            /**判断是左右项太长还是标题项太长*/
            if (sideWidth > barWidth / 3) {
                /**如果是左右项太长，那么按照比例进行划分*/
                mLeftView.maxWidth = barWidth / 4
                mTitleView.maxWidth = barWidth / 2
                mRightView.maxWidth = barWidth / 4
            } else {
                /**如果是标题项太长，那么就进行动态计算*/
                mLeftView.maxWidth = sideWidth
                mTitleView.maxWidth = barWidth - sideWidth * 2
                mRightView.maxWidth = sideWidth
            }
        } else {
            if (mLeftView.maxWidth != Int.MAX_VALUE && mTitleView.maxWidth != Int.MAX_VALUE && mRightView.maxWidth != Int.MAX_VALUE) {
                /**不限制子 View 的最大宽度*/
                mLeftView.maxWidth = Int.MAX_VALUE
                mTitleView.maxWidth = Int.MAX_VALUE
                mRightView.maxWidth = Int.MAX_VALUE
            }
        }

        /**TextView 里面必须有东西才能被点击*/
        mLeftView.isEnabled = TitleBarSupport.isContainContent(mLeftView)
        mTitleView.isEnabled = TitleBarSupport.isContainContent(mTitleView)
        mRightView.isEnabled = TitleBarSupport.isContainContent(mRightView)

        post {
            /**这里再次监听需要延迟，否则会导致递归的情况发生*/
            addOnLayoutChangeListener(this@TitleBar)
        }
    }

    override fun setLayoutParams(params: ViewGroup.LayoutParams) {
        if (params.width == LayoutParams.WRAP_CONTENT) {
            /**如果当前宽度是自适应则转换成占满父布局*/
            params.width = LayoutParams.MATCH_PARENT
        }

        val horizontalPadding = mHorizontalPadding
        var verticalPadding = 0
        /**如果当前高度是自适应则设置默认的内间距*/
        if (params.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
            verticalPadding = mVerticalPadding
        }
        setChildPadding(horizontalPadding, verticalPadding)
        super.setLayoutParams(params)
    }

    override fun generateDefaultLayoutParams(): LayoutParams {
        return LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
    }

    /**设置标题栏的点击监听器*/
    fun setOnTitleBarListener(listener: OnTitleBarListener): TitleBar {
        mListener = listener
        // 设置监听
        mTitleView.setOnClickListener(this)
        mLeftView.setOnClickListener(this)
        mRightView.setOnClickListener(this)
        return this
    }

    fun setTitle(id: Int): TitleBar {
        return setTitle(resources.getString(id))
    }

    fun setTitle(text: CharSequence): TitleBar {
        mTitleView.text = text
        return this
    }

    fun getTitle(): CharSequence {
        return mTitleView.text
    }

    /**设置左标题的文本*/
    fun setLeftTitle(id: Int): TitleBar {
        return setLeftTitle(resources.getString(id))
    }

    fun setLeftTitle(text: CharSequence): TitleBar {
        mLeftView.text = text
        return this
    }

    fun getLeftTitle(): CharSequence {
        return mLeftView.text
    }

    /**设置右标题的文本*/
    fun setRightTitle(id: Int): TitleBar {
        return setRightTitle(resources.getString(id))
    }

    fun setRightTitle(text: CharSequence): TitleBar {
        mRightView.text = text
        return this
    }

    fun getRightTitle(): CharSequence {
        return mRightView.text
    }

    fun setTitleStyle(style: Int): TitleBar {
        return setTitleStyle(TitleBarSupport.getTextTypeface(style), style)
    }

    /**
     * 设置标题样式
     *
     * @param typeface              字体样式
     * @param style                 文字样式
     */
    fun setTitleStyle(typeface: Typeface, style: Int): TitleBar {
        mTitleView.setTypeface(typeface, style)
        return this
    }

    fun setLeftTitleStyle(style: Int): TitleBar {
        return setLeftTitleStyle(TitleBarSupport.getTextTypeface(style), style)
    }

    /**
     * 设置左标题样式
     *
     * @param typeface              字体样式
     * @param style                 文字样式
     */
    fun setLeftTitleStyle(typeface: Typeface, style: Int): TitleBar {
        mLeftView.setTypeface(typeface, style)
        return this
    }

    fun setRightTitleStyle(style: Int): TitleBar {
        return setRightTitleStyle(TitleBarSupport.getTextTypeface(style), style)
    }

    /**
     * 设置右边标题样式
     *
     * @param typeface              字体样式
     * @param style                 文字样式
     */
    fun setRightTitleStyle(typeface: Typeface, style: Int): TitleBar {
        mRightView.setTypeface(typeface, style)
        return this
    }

    /**
     * 设置标题的字体颜色
     */
    fun setTitleColor(color: Int): TitleBar {
        return setTitleColor(ColorStateList.valueOf(color))
    }

    fun setTitleColor(color: ColorStateList): TitleBar {
        mTitleView.setTextColor(color)
        return this
    }

    /**
     * 设置左标题的字体颜色
     */
    fun setLeftTitleColor(color: Int): TitleBar {
        return setLeftTitleColor(ColorStateList.valueOf(color))
    }

    fun setLeftTitleColor(color: ColorStateList): TitleBar {
        mLeftView.setTextColor(color)
        return this
    }

    /**
     * 设置右标题的字体颜色
     */
    fun setRightTitleColor(color: Int): TitleBar {
        return setRightTitleColor(ColorStateList.valueOf(color))
    }

    fun setRightTitleColor(color: ColorStateList): TitleBar {
        mRightView.setTextColor(color)
        return this
    }

    /**
     * 设置标题的字体大小
     */
    fun setTitleSize(size: Int): TitleBar {
        return setTitleSize(TypedValue.COMPLEX_UNIT_SP, size)
    }

    fun setTitleSize(unit: Int, size: Int): TitleBar {
        mTitleView.setTextSize(unit, size.toFloat())
        return this
    }

    /**
     * 设置左标题的字体大小
     */
    fun setLeftTitleSize(size: Float): TitleBar {
        return setLeftTitleSize(TypedValue.COMPLEX_UNIT_SP, size.toInt())
    }

    fun setLeftTitleSize(unit: Int, size: Int): TitleBar {
        mLeftView.setTextSize(unit, size.toFloat())
        return this
    }

    /**
     * 设置右标题的字体大小
     */
    fun setRightTitleSize(size: Float): TitleBar {
        return setRightTitleSize(TypedValue.COMPLEX_UNIT_SP, size.toInt())
    }

    fun setRightTitleSize(unit: Int, size: Int): TitleBar {
        mRightView.setTextSize(unit, size.toFloat())
        return this
    }

    /**
     * 设置标题的图标
     */
    fun setTitleIcon(id: Int): TitleBar {
        return setTitleIcon(TitleBarSupport.getDrawable(context, id))
    }

    fun setTitleIcon(drawable: Drawable): TitleBar {
        TitleBarSupport.setDrawableTint(drawable, mTitleIconTint)
        TitleBarSupport.setDrawableSize(drawable, mTitleIconWidth, mTitleIconHeight)
        TitleBarSupport.setTextCompoundDrawable(mTitleView, drawable, mTitleIconGravity)
        return this
    }

    fun getTitleIcon(): Drawable? {
        return TitleBarSupport.getTextCompoundDrawable(mTitleView, mTitleIconGravity)
    }

    /**
     * 设置左标题的图标
     */
    fun setLeftIcon(id: Int): TitleBar {
        return setLeftIcon(TitleBarSupport.getDrawable(context, id))
    }

    fun setLeftIcon(drawable: Drawable): TitleBar {
        TitleBarSupport.setDrawableTint(drawable, mLeftIconTint)
        TitleBarSupport.setDrawableSize(drawable, mLeftIconWidth, mLeftIconHeight)
        TitleBarSupport.setTextCompoundDrawable(mLeftView, drawable, mLeftIconGravity)
        return this
    }

    fun getLeftIcon(): Drawable? {
        return TitleBarSupport.getTextCompoundDrawable(mLeftView, mLeftIconGravity)
    }

    /**
     * 设置右标题的图标
     */
    fun setRightIcon(id: Int): TitleBar {
        return setRightIcon(TitleBarSupport.getDrawable(context, id))
    }

    fun setRightIcon(drawable: Drawable): TitleBar {
        TitleBarSupport.setDrawableTint(drawable, mRightIconTint)
        TitleBarSupport.setDrawableSize(drawable, mRightIconWidth, mRightIconHeight)
        TitleBarSupport.setTextCompoundDrawable(mRightView, drawable, mRightIconGravity)
        return this
    }

    fun getRightIcon(): Drawable? {
        return TitleBarSupport.getTextCompoundDrawable(mRightView, mRightIconGravity)
    }

    /**
     * 设置标题的图标大小
     */
    fun setTitleIconSize(width: Int, height: Int): TitleBar {
        mTitleIconWidth = width
        mTitleIconHeight = height
        TitleBarSupport.setDrawableSize(getTitleIcon(), width, height)
        return this
    }

    /**
     * 设置左标题的图标大小
     */
    fun setLeftIconSize(width: Int, height: Int): TitleBar {
        mLeftIconWidth = width
        mLeftIconHeight = height
        TitleBarSupport.setDrawableSize(getLeftIcon(), width, height)
        return this
    }

    /**
     * 设置右标题的图标大小
     */
    fun setRightIconSize(width: Int, height: Int): TitleBar {
        mRightIconWidth = width
        mRightIconHeight = height
        TitleBarSupport.setDrawableSize(getRightIcon(), width, height)
        return this
    }

    /**
     * 设置标题的文字和图标间距
     */
    fun setTitleIconPadding(padding: Int): TitleBar {
        mTitleView.compoundDrawablePadding = padding
        return this
    }

    /**
     * 设置左标题的文字和图标间距
     */
    fun setLeftIconPadding(padding: Int): TitleBar {
        mLeftView.compoundDrawablePadding = padding
        return this
    }

    /**
     * 设置右标题的文字和图标间距
     */
    fun setRightIconPadding(padding: Int): TitleBar {
        mRightView.compoundDrawablePadding = padding
        return this
    }

    /**
     * 设置标题的图标着色器
     */
    fun setTitleIconTint(color: Int): TitleBar {
        mTitleIconTint = color
        TitleBarSupport.setDrawableTint(getTitleIcon(), color)
        return this
    }

    /**
     * 设置左标题的图标着色器
     */
    fun setLeftIconTint(color: Int): TitleBar {
        mLeftIconTint = color
        TitleBarSupport.setDrawableTint(getLeftIcon(), color)
        return this
    }

    /**
     * 设置右标题的图标着色器
     */
    fun setRightIconTint(color: Int): TitleBar {
        mRightIconTint = color
        TitleBarSupport.setDrawableTint(getRightIcon(), color)
        return this
    }

    /**
     * 清楚标题的图标着色器
     */
    fun clearTitleIconTint(): TitleBar {
        mTitleIconTint = TitleBarSupport.NO_COLOR
        TitleBarSupport.clearDrawableTint(getTitleIcon())
        return this
    }

    /**
     * 清楚左标题的图标着色器
     */
    fun clearLeftIconTint(): TitleBar {
        mLeftIconTint = TitleBarSupport.NO_COLOR
        TitleBarSupport.clearDrawableTint(getLeftIcon())
        return this
    }

    /**
     * 清楚右标题的图标着色器
     */
    fun clearRightIconTint(): TitleBar {
        mRightIconTint = TitleBarSupport.NO_COLOR
        TitleBarSupport.clearDrawableTint(getRightIcon())
        return this
    }

    /**
     * 设置标题的图标显示重心
     */
    fun setTitleIconGravity(gravity: Int): TitleBar {
        val drawable = getTitleIcon()
        mTitleIconGravity = gravity
        drawable?.let {
            TitleBarSupport.setTextCompoundDrawable(
                mTitleView,
                it,
                gravity
            )
        }
        return this
    }

    /**
     * 设置左标题的图标显示重心
     */
    fun setLeftIconGravity(gravity: Int): TitleBar {
        val drawable = getLeftIcon()
        mLeftIconGravity = gravity
        drawable?.let {
            TitleBarSupport.setTextCompoundDrawable(
                mLeftView,
                drawable,
                gravity
            )
        }
        return this
    }

    /**
     * 设置右标题的图标显示重心
     */
    fun setRightIconGravity(gravity: Int): TitleBar {
        val drawable = getRightIcon()
        mRightIconGravity = gravity
        drawable?.let {
            TitleBarSupport.setTextCompoundDrawable(
                mRightView,
                drawable,
                gravity
            )
        }
        return this
    }

    /**
     * 设置左标题的背景状态选择器
     */
    fun setLeftBackground(id: Int): TitleBar {
        return setLeftBackground(TitleBarSupport.getDrawable(context, id))
    }

    fun setLeftBackground(drawable: Drawable): TitleBar {
        TitleBarSupport.setBackground(mLeftView, drawable)
        return this
    }

    /**
     * 设置右标题的背景状态选择器
     */
    fun setRightBackground(id: Int): TitleBar {
        return setRightBackground(TitleBarSupport.getDrawable(context, id))
    }

    fun setRightBackground(drawable: Drawable): TitleBar {
        TitleBarSupport.setBackground(mRightView, drawable)
        return this
    }

    /**
     * 设置分割线是否显示
     */
    fun setLineVisible(visible: Boolean): TitleBar {
        mLineView.visibility = if (visible) VISIBLE else INVISIBLE
        return this
    }

    /**
     * 设置分割线的颜色
     */
    fun setLineColor(color: Int): TitleBar {
        return setLineDrawable(ColorDrawable(color))
    }

    fun setLineDrawable(drawable: Drawable): TitleBar {
        TitleBarSupport.setBackground(mLineView, drawable)
        return this
    }

    /**
     * 设置分割线的大小
     */
    fun setLineSize(px: Int): TitleBar {
        val layoutParams = mLineView.layoutParams
        layoutParams.height = px
        mLineView.layoutParams = layoutParams
        return this
    }

    /**
     * 设置标题重心
     */
    @SuppressLint("RtlHardcoded")
    fun setTitleGravity(gravity: Int): TitleBar {
        var gravity = gravity
        gravity = TitleBarSupport.getAbsoluteGravity(this, gravity)

        // 如果标题的重心为左，那么左边就不能有内容
        if (gravity == Gravity.LEFT && (if (TitleBarSupport.isLayoutRtl(context)) mRightView else mLeftView).let {
                TitleBarSupport.isContainContent(
                    it
                )
            }
        ) {
            /**Title center of gravity for the left, the left title can not have content*/
            return this
        }

        // 如果标题的重心为右，那么右边就不能有内容
        if (gravity == Gravity.RIGHT && (if (TitleBarSupport.isLayoutRtl(
                    context
                )
            ) mLeftView else mRightView).let {
                TitleBarSupport.isContainContent(
                    it
                )
            }
        ) {
            /**Title center of gravity for the right, the right title can not have content*/
            return this
        }
        val params = mTitleView.layoutParams as LayoutParams
        params.gravity = gravity
        mTitleView.layoutParams = params
        return this
    }

    /**
     * 设置子 View 内间距
     */
    fun setChildPadding(horizontalPadding: Int, verticalPadding: Int): TitleBar {
        mHorizontalPadding = horizontalPadding
        mVerticalPadding = verticalPadding
        mLeftView.setPadding(
            horizontalPadding,
            verticalPadding,
            horizontalPadding,
            verticalPadding
        )
        mTitleView.setPadding(
            horizontalPadding,
            verticalPadding,
            horizontalPadding,
            verticalPadding
        )
        mRightView.setPadding(
            horizontalPadding,
            verticalPadding,
            horizontalPadding,
            verticalPadding
        )
        return this
    }

    /**
     * 获取左标题View对象
     */
    fun getLeftView(): TextView {
        return mLeftView
    }

    /**
     * 获取标题View对象
     */
    fun getTitleView(): TextView {
        return mTitleView
    }

    /**
     * 获取右标题View对象
     */
    fun getRightView(): TextView {
        return mRightView
    }

    /**
     * 获取分割线View对象
     */
    fun getLineView(): View {
        return mLineView
    }

    /**
     * 获取当前的初始化器
     */
    fun getCurrentStyle(): ITitleBarStyle {
        return mCurrentStyle
    }

    /**
     * 设置默认初始化器
     */
    fun setDefaultStyle(style: ITitleBarStyle) {
        TitleBar.sGlobalStyle = style
    }

}